package cn.cvs.utils;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import test.cn.test.demo;

import java.util.Arrays;

@Aspect
public class PointCutUtil {
    private static final Logger logger = Logger.getLogger(demo.class);

    @Pointcut("execution()")
    public void pointcutMeanger(){

    }

    public void loggerAspect(JoinPoint jn){
        logger.info("调用了"+jn.getTarget().toString()+"类中的"+jn.getSignature().getName()
                +"方法,参数有"+ Arrays.toString(jn.getArgs()));
    }
    public void loggerError(JoinPoint jp,RuntimeException ex){

        logger.error("调用了"+jp.getTarget().toString()+"类中的"+jp.getSignature().getName()
                +"方法,参数有"+ Arrays.toString(jp.getArgs())+"异常信息为"+ex.getMessage());
    }
    public void loggerFinally(JoinPoint jp){
        logger.info("调用了"+jp.getTarget().toString()+"类中的"+jp.getSignature().getName()
                +"方法,参数有"+ Arrays.toString(jp.getArgs())+"方法执行完毕");
    }
    public void loggerAround(ProceedingJoinPoint jp){
        logger.info("调用了"+jp.getTarget().toString()+"类中的"+jp.getSignature().getName()
                +"方法,参数有"+ Arrays.toString(jp.getArgs()));
        try{
            Object proceed = jp.proceed(jp.getArgs());
        } catch (Throwable ex) {
            logger.error("调用了"+jp.getTarget().toString()+"类中的"+jp.getSignature().getName()
                    +"方法,参数有"+ Arrays.toString(jp.getArgs())+"异常信息为"+ex.getMessage());
        }finally {
            logger.info("调用了"+jp.getTarget().toString()+"类中的"+jp.getSignature().getName()
                    +"方法,参数有"+ Arrays.toString(jp.getArgs())+"方法执行完毕");
        }
    }
}
